package personal.gavin.gtweet.effect
{
	import flash.display.InteractiveObject;
	import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.filters.GlowFilter;
    public class GlowTween
    {
        private var _target:InteractiveObject;
        private var _color:uint;
        private var _toggle:Boolean;
        private var _blur:Number;
        private var _blurMax:Number;
        private var _blurMin:Number;
        private var _inner:Boolean;
        
        public function GlowTween(target:InteractiveObject, color:uint = 0xFFFFFF, blurMin:Number = 2, blurMax:Number = 10 ,inner:Boolean = false)
        {
            _target = target;
            _color = color;
            _toggle = true;
            _blur = blurMin;
            _blurMin = blurMin;
            _blurMax = blurMax;
            _inner = inner;
            target.addEventListener(MouseEvent.ROLL_OVER, startGlowHandler);
            target.addEventListener(MouseEvent.ROLL_OUT, stopGlowHandler);
        }
 
        public function remove():void
        {
            _target.removeEventListener(MouseEvent.ROLL_OVER, startGlowHandler);
            _target.removeEventListener(MouseEvent.ROLL_OUT, stopGlowHandler);
            _target.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
            _target.filters = null;
            _target = null;
        }
 
        private function startGlowHandler(event:MouseEvent):void
        {
            _target.addEventListener(Event.ENTER_FRAME, onEnterFrame, false, 0, true);
        }
 
        private function stopGlowHandler(event:MouseEvent):void
        {
            _target.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
            _target.filters = null;
        }
 
        private function onEnterFrame(event:Event):void
        {
        if (_blur >= _blurMax)
        {
            _toggle = false;
        }
        else if (_blur <= _blurMin)
        {    
            _toggle = true;
        }
        _toggle ? _blur++ : _blur--;
        var glow:GlowFilter = new GlowFilter(_color, 1, _blur, _blur, 2, 2, _inner);
        _target.filters = [glow];
        }
    }
}